//
//  MainViewController.m
//  action设计模式
//
//  Created by Scott on 15/4/22.
//  Copyright (c) 2015年 www.lanou3g.com 蓝欧3G. All rights reserved.
//

#import "MainViewController.h"
#import "TView.h"
#import "TouchView.h"


@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#pragma mark - 知识点1 target/action 设计模式
/**
 * @brief 讲解
 *
 * 每个视图的点击事件都一样, 如何处理
 * 无法预先知道这个视图点击之后都要实现什么效果
 * 因此在类的内部提前写好点击事件不科学.
 */
    
    /* 复习之前的触摸事件.
     * 定义个UIView的子类(TView). 类中实现touchend的方法(点击结束后, 将背景颜色变为黄色);
     * 在MainViewController中, 创建两个TView对象, 当点击每个TView的时候, 点击事件是一样的.
     */
    
    TView *tview1 = [[TView alloc] initWithFrame:CGRectMake(20, 60, self.view.frame.size.width - 40, 100)];
    tview1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:tview1];
    [tview1 release];
    
    
    TView *tview2 = [[TView alloc] initWithFrame:CGRectMake(20, 180, self.view.frame.size.width - 40, 100)];
    tview2.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:tview2];
    [tview2 release];
    
    /* 这个时候点击两个TView 对象, 行为都一样, 即都是将背景颜色变为了红色. 如果要实现第一个点击之后变为黄色, 第二个点击变为红色呢? 如何实现呢? */
    
    /* target/action 设计模式 
     * 在新的UIView子类的.h 文件中添加两个属性: id 和 SEL
     */
    
    /* 定义一个新的UIView子类(TouchView), 重写touchend点击的方法,但里面的实现不要写固定  
     */
    
    TouchView *touch1View = [[TouchView alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width - 40, 100)];
    touch1View.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:touch1View];
    
    /* 设置touch1View的target 和 action */
    touch1View.target = self;
    touch1View.action = @selector(touch1Clicked:);
    [touch1View release];
    
    
    TouchView *touch2View = [[TouchView alloc] initWithFrame:CGRectMake(20, 400, self.view.frame.size.width - 40, 100)];
    touch2View.backgroundColor = [UIColor blueColor];
    [self.view addSubview:touch2View];
    /* 设置touch2view的target 和 action */
//    touch2View.target = self;
//    touch2View.action = @selector(touch2Clicked:);
    [touch2View addTarget:self action:@selector(touch2Clicked:)];
    
    [touch2View release];
    
    /**
     * @brief 总结
     * 通过target/action 模式, 每个视图的点击事件可以自定义
     */
    
}

/* 实现 touch1Clicked 方法 */
- (void)touch1Clicked:(TouchView *)touchview
{
    touchview.backgroundColor = [UIColor yellowColor];
}

/* 实现 touch2Clicked 方法 */
- (void)touch2Clicked:(TouchView *)touchView
{
    touchView.backgroundColor = [UIColor redColor];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
